package com.example.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.Model.music.MusicComment;
import com.example.UI.Mydialog;

import com.example.utils.GlideCircleTransform;

import java.util.ArrayList;

import sdstcom.lbq.one.R;

/**
 * Created by Administrator on 2017/1/10.
 */

public class MusicComAdapter extends BaseAdapter{
    private ArrayList<MusicComment.DataBeanX.DataBean> data;
    private LayoutInflater inflater;
    private Context context;

    public MusicComAdapter(ArrayList<MusicComment.DataBeanX.DataBean> data, Context context) {
        this.data = data;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

       ViewHolder holder = null;
        ViewHolder2 holder2 = null;
        if (getItemViewType(position) == 1) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_essaycomment, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            //设置评论者头像
            GlideCircleTransform transform = new GlideCircleTransform(context);
            Glide.with(context).load(data.get(position).getUser().getWeb_url()).transform(transform).into(holder.commenter);
            //设置评论者名字
            holder.commentername.setText(data.get(position).getUser().getUser_name());
            //设置评论日期
            holder.commentredate.setText(data.get(position).getInput_date());
            //设置评论内容
            holder.commentcontent.setText(data.get(position).getContent());
            //设置评论点赞数
            holder.prisacount.setText(data.get(position).getPraisenum() + "");

            //设置评论对话框
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String content = new String();
                    if(data.get(position).getContent().length()<=16){
                        content = data.get(position).getContent();
                    }else {
                        content = data.get(position).getContent().substring(0,16);
                    }
                    Mydialog dialog = new Mydialog(context, data.get(position).getUser().getUser_name(), content + "...");
                    dialog.showdialog();
                }
            });




        } else {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item2_essaycomment, null);
                holder2 = new ViewHolder2(convertView);
                convertView.setTag(holder2);
            } else {
                holder2 = (ViewHolder2) convertView.getTag();
            }
            //设置评论者头像
            GlideCircleTransform transform = new GlideCircleTransform(context);
            Glide.with(context).load(data.get(position).getUser().getWeb_url()).transform(transform).into(holder2.commenter);
            //设置评论者名字
            holder2.commentername.setText(data.get(position).getUser().getUser_name());
            //设置评论日期
            holder2.commentredate.setText(data.get(position).getInput_date());


            //设置回复评论者名字
            holder2.commentedname.setText(data.get(position).getTouser().getUser_name());
            //设置回复评论内容
            holder2.commentedcontent.setText(data.get(position).getQuote());

            //设置评论内容
            holder2.commentcontent.setText(data.get(position).getContent());
            //为评论设置监听，实现拷贝和举报功能
            final View contentview = inflater.inflate(R.layout.popup,null);
            final PopupWindow window = new PopupWindow(contentview,180,80);
            window.setOutsideTouchable(true);
            window.setFocusable(true);
            final View parentview = inflater.inflate(R.layout.activity_main,null);
            holder2.commentcontent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    window.showAtLocation(parentview, Gravity.CENTER,0,0);
                }
            });
            //设置评论点赞数
            holder2.prisacount.setText(data.get(position).getPraisenum() + "");
            //设置评论对话框
            holder2.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String content = new String();
                    if(data.get(position).getContent().length()<=16){
                        content = data.get(position).getContent();
                    }else {
                        content = data.get(position).getContent().substring(0,16);
                    }
                    Mydialog dialog = new Mydialog(context, data.get(position).getUser().getUser_name(), content + "...");
                    dialog.showdialog();
                }
            });


        }

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position).getTouser() != null) {
            return 0;
        } else {
            return 1;
        }
    }

    public class ViewHolder {
        private ImageView commenter;
        private TextView commentername;
        private TextView commentredate;
        private TextView commentcontent;
        private TextView prisacount;
        private Button button;


        public ViewHolder(View convertview) {
            commenter = (ImageView) convertview.findViewById(R.id.essay_comment_iv);
            commentername = (TextView) convertview.findViewById(R.id.essay_commentername_tv);
            commentredate = (TextView) convertview.findViewById(R.id.essay_comment_date);
            commentcontent = (TextView) convertview.findViewById(R.id.essay_commentcontent_tv);
            prisacount = (TextView) convertview.findViewById(R.id.essay_pri_tv);
            button = (Button) convertview.findViewById(R.id.comment_btn);
        }
    }

    public class ViewHolder2 {
        private ImageView commenter;
        private TextView commentername;
        private TextView commentredate;
        private TextView commentcontent;
        private TextView prisacount;
        private TextView commentedname;
        private TextView commentedcontent;
        private Button button;

        public ViewHolder2(View convertview) {
            commenter = (ImageView) convertview.findViewById(R.id.essay_comment_iv2);
            commentername = (TextView) convertview.findViewById(R.id.essay_commentername2_tv);
            commentredate = (TextView) convertview.findViewById(R.id.essay_comment_date2);
            commentcontent = (TextView) convertview.findViewById(R.id.essay_commentcontent_tv2);
            prisacount = (TextView) convertview.findViewById(R.id.essay_zancount_tv2);
            commentedname = (TextView) convertview.findViewById(R.id.commented_name_tv);
            commentedcontent = (TextView) convertview.findViewById(R.id.commented_content_tv);
            button = (Button) convertview.findViewById(R.id.comment_btn2);
        }
    }
}
